package com.ttn.common.core.util;

import cn.hutool.core.date.DateTime;
import cn.hutool.core.date.DateUtil;
import cn.hutool.core.lang.Snowflake;
import cn.hutool.core.util.IdUtil;
import cn.hutool.core.util.RandomUtil;

import java.time.ZoneId;
import java.time.format.DateTimeFormatter;
import java.util.Locale;

public class IdGenerator {
    private static Snowflake salesOrder = IdUtil.getSnowflake(1, 1);
    private static Snowflake productionOrder = IdUtil.getSnowflake(1, 1);
    private static Snowflake productQuality = IdUtil.getSnowflake(1, 1);

    /**
     * 获取销售订单id
     *
     * @return
     */
    public static String generatorSalesOrderId() {
        return salesOrder.nextIdStr();

    }

    /**
     * 获取生产订单
     *
     * @return
     */
    public static String generatorProductionOrderId() {
        return productionOrder.nextIdStr();
    }

    /**
     * 获取批次号
     *
     * @return
     */
    public static String generatorProductBatchNumber() {
        return DateUtil.format(DateTime.now(), createFormatter("yyMMdd")) + RandomUtil.randomInt(100, 999);
    }

    /**
     * 获取测试编号
     *
     * @return
     */
    public static String generatorProductQualityId() {
        return productQuality.nextIdStr();
    }

    /**
     * 获取测试标识
     *
     * @return
     */
    public static String generatorTestId() {
        return IdUtil.fastSimpleUUID();
    }

    private static DateTimeFormatter createFormatter(String pattern) {
        return DateTimeFormatter.ofPattern(pattern, Locale.getDefault()).withZone(ZoneId.systemDefault());
    }

    /**
     * 生成设备uuid
     *
     * @return
     */
    public static String generatorUuid(String productId, String manufacturerId) {
        return productId + manufacturerId + DateUtil.format(DateTime.now(), createFormatter("yyMMdd"));
    }

    /**
     * 生成设备uuid二
     *
     * @return
     */
    public static String generatorUuid() {
        return IdUtil.simpleUUID().toUpperCase();
    }

    public static void main(String[] args) {
        System.out.println(generatorUuid());
    }
}
